package org.eclipse.emf.index.omds.core.query;

import java.io.Serializable;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import javax.swing.text.StyleContext.SmallAttributeSet;

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.index.EObjectDescriptor;
import org.eclipse.emf.index.ResourceDescriptor;
import org.eclipse.emf.index.ecore.EClassDescriptor;
import org.eclipse.emf.index.omds.ui.DialogBoxCreator;
import org.eclipse.emf.index.omds.ui.content.OMSLableProvider;
import org.eclipse.emf.index.omds.ui.content.OMSResourceObject;
import org.eclipse.emf.index.omds.ui.control.NamesSeperator;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.PlatformUI;

public class QueryGateway {
	
	private ArrayList resourcesList;
	private OMSResourceObject[] resourceObjects;

	public OMSResourceObject[] getContent(){		
		//Get the List of resources from the index.
		QueryIndex queryIndex = new QueryIndex();
		resourcesList = queryIndex.getIndex();
		resourceObjects = this.getResources();		
		return resourceObjects;
	}

	private OMSResourceObject[] getResources() {
		OMSResourceObject[] omsResourceObjects = new OMSResourceObject[resourcesList.size()];
		EObjectDescriptor sampleEobject = null;
		HashMap packageMap = new HashMap();
		String packageName = null;
		String resourceName = null;
		for(int i=0;i<resourcesList.size();i++){
			sampleEobject = (EObjectDescriptor)resourcesList.get(i);
			omsResourceObjects[i] = new OMSResourceObject();
			
			//If there are listeners Present the display name is changed.
			if("Listener".equalsIgnoreCase(sampleEobject.getEClassDescriptor().getName())){
				String tempDisplayName = sampleEobject.getDisplayName();
				int m = tempDisplayName.lastIndexOf('.');
				resourceName = tempDisplayName.substring(m+1);
				omsResourceObjects[i].setResourceName(resourceName);
			}
			else{
				resourceName = sampleEobject.getDisplayName();
				omsResourceObjects[i].setResourceName(resourceName);
			}
			omsResourceObjects[i].setResourceType(sampleEobject.getEClassDescriptor().getName());
			omsResourceObjects[i].setResourceUri(sampleEobject.getResourceDescriptor().getURI());
			NamesSeperator seperator = new NamesSeperator((String)sampleEobject.getUserData(resourceName));
			omsResourceObjects[i].setPackageName(seperator.getPackageName());
			omsResourceObjects[i].setProjectName(seperator.getProjectName());
		}
		return omsResourceObjects;
	}                             

}
